package com.ntd.study.base.thread;

import cn.hutool.core.lang.Console;

/**
 * 线程锁学习
 * synchronized
 * 1.同步代码块
 * 2.同步方法
 * 3.不可中断锁
 * 4.非公平锁 - 不分先后顺序，看CPU心情
 * 对象锁：同一个对象能
 */
public class DemoThreadSynchronized {
    private static DemoThreadSynchronized dThreadLock = new DemoThreadSynchronized();

    static class Dthread1 implements Runnable {
        public void run() {
            Console.log(Thread.currentThread().getName());
            DemoThreadSynchronized.classLock();
            dThreadLock.objectLock();
            dThreadLock.synchronizedLock();
        }
    }

    private synchronized void objectLock() {
        Console.log("同步方法-对象锁-START");
    }

    private static synchronized void classLock() {
        Console.log("同步方法-类锁-START");
    }

    private synchronized void synchronizedLock() {
        synchronized (this) {
            Console.log("同步代码块-对象锁-START");
        }
        synchronized (DemoThreadSynchronized.class) {
            Console.log("同步代码块-类锁-START");
        }
    }

    public static void main(String[] args) {
        final Dthread1 dthread1 = new Dthread1();
        new Thread(dthread1).start();
        new Thread(dthread1).start();
        new Thread(dthread1).start();
        new Thread(dthread1).start();
    }


}
